package com.github.xaidanwang.auth.server.config;

import com.github.xaidanwang.auth.server.auth.phone.*;
import com.github.xaidanwang.auth.server.handler.CustomLogoutHandler;
import com.github.xaidanwang.auth.server.handler.CustomLogoutSuccessHandler;
import com.github.xaidanwang.auth.server.handler.LoginFailureHandler;
import com.github.xaidanwang.auth.server.handler.LoginSuccessHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.ProviderManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import java.util.Collections;

/**
 * @author wang yi fei
 * @date 2019/9/30 17:00
 */
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
	@Autowired
	@Qualifier("userDetailsService")
	private UserDetailsService userDetailsService;
	@Autowired
	@Qualifier("phoneUserDetailService")
	private PhoneUserDetailService phoneUserDetailService;
	@Autowired
	private LoginSuccessHandler loginSuccessHandler;
	@Autowired
	private LoginFailureHandler loginFailureHandler;
	@Autowired
	private PhoneSuccessHandler phoneSuccessHandler;
	@Autowired
	private PhoneFailureHandler phoneFailureHandler;
	@Autowired
	private CustomLogoutHandler logoutHandler;
	@Autowired
	private CustomLogoutSuccessHandler logoutSuccessHandler;

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable()
				.authorizeRequests()
				.mvcMatchers("/login","/phone").permitAll();
		http.formLogin().successHandler(loginSuccessHandler).failureHandler(loginFailureHandler);
		http.logout().addLogoutHandler(logoutHandler).logoutSuccessHandler(logoutSuccessHandler);

		// 手机号码登陆
		PhoneAuthenticationProvider phoneAuthenticationProvider = new PhoneAuthenticationProvider(phoneUserDetailService);
		ProviderManager phoneProviderManager =
				new ProviderManager(Collections.singletonList(phoneAuthenticationProvider));
		PhoneAuthenticationFilter phoneAuthenticationFilter = new PhoneAuthenticationFilter();
		phoneAuthenticationFilter.setAuthenticationManager(phoneProviderManager);
		phoneAuthenticationFilter.setAuthenticationSuccessHandler(phoneSuccessHandler);
		phoneAuthenticationFilter.setAuthenticationFailureHandler(phoneFailureHandler);
		http.addFilterBefore(phoneAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
		super.configure(http);
	}


	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());
	}

	@Bean
	public PasswordEncoder passwordEncoder() {
		return new BCryptPasswordEncoder();
	}

/*	public PhoneAuthenticationFilter phoneAuthenticationFilter(){
		PhoneAuthenticationFilter phoneAuthenticationFilter = new PhoneAuthenticationFilter();
		phoneAuthenticationFilter.setUsernameParameter("phone");
		phoneAuthenticationFilter.setPostOnly(true);
		phoneAuthenticationFilter.setPasswordParameter("password");
		return phoneAuthenticationFilter;
	}*/
}
